package com.ebai.product.management.base.security.service;

import com.ebai.product.management.base.cache.service.MessageCacheImpl;
import com.ebai.product.management.base.security.BasicSecurityManager;
import com.ebai.product.management.base.utils.Common;
import com.ebai.product.management.base.utils.SysCode;
import com.ebai.product.management.model.entity.User;
import com.ebai.product.management.service.system.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Component("basicSecurityManager")
public class BasicSecurityService implements BasicSecurityManager {

    @Autowired
    private UserService userService;

	public void setCurrentUser(HttpSession session, User user) {
		try {
			// 清空session对象
			//session.invalidate();
			session.setAttribute("EBAI_CURRENTUSER", user);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	public User getCurrentUser(HttpSession session) {
		User user = null;
		
		Object obj = session.getAttribute("EBAI_CURRENTUSER");
		if (null != obj) {
			user = (User)obj;
		}
		
		return user;
	}

    /**
     * 执行用户登录验证
     * @param session 会话
     * @param user 登录用户
     * @return 验证结果
     */
    public Map<String,Object> chkLogin(HttpSession session, User user) {
        Map<String,Object> map = new HashMap<String,Object>();
        if (StringUtils.isBlank(user.getLoginName()) || StringUtils.isBlank(user.getLoginPasswd())) {
            map.put("success", false);
            map.put("data", "登录名称或登录密码错误(或用户不存在)！");
            return map;
        }

        user.setLoginPasswd(Common.EncryptMD5(user.getLoginPasswd() + SysCode.DEFAULT_USER.SALT, Common.MD5_LX_32));
        User currentUser = this.userService.queryByLoginName(user.getLoginName(), user.getLoginPasswd());
        if (null == currentUser) {
            map.put("success", false);
            map.put("data", "登录名称或登录密码错误(或用户不存在)！");
        }
        else {
            if (StringUtils.equalsIgnoreCase(SysCode.DICT_TYSF.NO, currentUser.getIsEnable())) {
                map.put("success", false);
                map.put("data", "当前用户已禁用，请与管理员联系！");
            }
            else {
                this.setCurrentUser(session, currentUser);
//                TMisLogs log = new TMisLogs();
//                log.setLogType("0");
//                log.setLogInfo("用户正常登录系统！");
//                log.setOperUser(currentUser.getLoginName());
//                log.setOperDatetime(new Date());
//                try {
//                    this.logsDao.saveTMisLogs(log);
//                }
//                catch (Exception e) {
//                    // TODO 此处异常暂不做处理
//                }

                map.put("success", true);
                map.put("data", "");
            }
        }

        return map;
    }

    /**
     * 执行修改当前用户密码
     * @param session 会话
     * @param oldPasswd 旧密码
     * @param newPasswd 新密码
     * @return 执行结果
     */
    public Map<String,Object> modifyPasswd(HttpSession session, String oldPasswd, String newPasswd) {
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("success", false);
        result.put("msg", "");

        User user = this.getCurrentUser(session);

        oldPasswd = Common.EncryptMD5(oldPasswd + SysCode.DEFAULT_USER.SALT, Common.MD5_LX_32);
        // 原始密码不正确
        if (!StringUtils.equals(oldPasswd, user.getLoginPasswd())) {
            result.put("msg", MessageCacheImpl.getMsg("BasicSecurityService.modifyPasswd.Message1"));
        }
        else {
            try {
                this.userService.changePwd(user.getPid(), newPasswd);
                this.setCurrentUser(session, this.userService.queryByLoginName(user.getLoginName()));
                result.put("success", true);
                result.put("msg", MessageCacheImpl.getMsg("BasicSecurityService.modifyPasswd.Message2"));
            }
            catch(Exception e) {
                e.printStackTrace();
                result.put("msg", MessageCacheImpl.getMsg("BasicSecurityService.modifyPasswd.Message3"));
            }
        }

        return result;
    }
	
}
